﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EasyUIDemo.MVC.Controllers
{
    public class PhotosController : Controller
    {
        //
        // GET: /Photos/
        public ActionResult index(int? id)
        {
            if (id == null)
            {
                return Content("<script>alert('未获取到编号');</script>");
            }
            ViewBag.id = id;

            return View();
        }
        public ActionResult hu()
        {
            
            return View();
        }
        /// <summary>
        /// 测试数据
        /// </summary>
        /// <returns></returns>
        public string dd()
        {
            List<dynamic> list = new List<dynamic>();
            list.Add(new { id = 1, name = "111" });
            list.Add(new { id = 2, name = "112" });
            list.Add(new { id = 3, name = "113" });
            list.Add(new { id = 4, name = "114" });
            return Newtonsoft.Json.JsonConvert.SerializeObject(list) ;
        }
        
       [HttpPost]
        public JsonResult index(HttpPostedFileBase file)
        {
            
            if (file == null)
            {
                return Json(new
                {
                    bRet = false,
                    sMsg = "请选择图片！"
                },JsonRequestBehavior.AllowGet);
            }
            //上传文件代码 记得先新建一下 Upload 文件夹
            var fileName = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName));
            try
            {
                file.SaveAs(fileName);

                return Json(new
                {
                    bRet = true,
                    sMsg = "上传成功",
                    data=fileName
                });
            }
            catch
            {
                return Json(new
                {
                    bRet = false,
                    sMsg = "上传失败"
                });
            }

        }
    }
}
